﻿@model OrderModel
@inject Grand.Services.Security.IPermissionService permisionService;
<vc:admin-widget widget-zone="order_details_address_top" additional-data="Model" />
<div class="card col-md-6">
    <div class="card-body">
        <h4 class="card-title">@T("Admin.Orders.BillingInfo")</h4>
        <p class="card-text">
            <table class="table table-bordered table-striped table-hover">
                @if (Model.BillingAddress.FirstNameEnabled || Model.BillingAddress.LastNameEnabled)
                {
                    <tr>
                        <td style="width:25%">
                            @T("Admin.Orders.Address.FullName"):
                        </td>
                        <td>
                            @Model.BillingAddress.FirstName @Model.BillingAddress.LastName
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.EmailEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.Email"):
                        </td>
                        <td>
                            @Model.BillingAddress.Email
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.PhoneEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.Phone"):
                        </td>
                        <td>
                            @Model.BillingAddress.PhoneNumber
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.FaxEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.Fax"):
                        </td>
                        <td>
                            @Model.BillingAddress.FaxNumber
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.CompanyEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.Company"):
                        </td>
                        <td>
                            @Model.BillingAddress.Company
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.VatNumberEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.VatNumber"):
                        </td>
                        <td>
                            @Model.BillingAddress.VatNumber
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.StreetAddressEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.Address1"):
                        </td>
                        <td>
                            @Model.BillingAddress.Address1
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.StreetAddress2Enabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.Address2"):
                        </td>
                        <td>
                            @Model.BillingAddress.Address2
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.CityEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.City"):
                        </td>
                        <td>
                            @Model.BillingAddress.City
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.StateProvinceEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.StateProvince"):
                        </td>
                        <td>
                            @Model.BillingAddress.StateProvinceName
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.ZipPostalCodeEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.ZipPostalCode"):
                        </td>
                        <td>
                            @Model.BillingAddress.ZipPostalCode
                        </td>
                    </tr>
                }
                @if (Model.BillingAddress.CountryEnabled)
                {
                    <tr>
                        <td>
                            @T("Admin.Orders.Address.Country"):
                        </td>
                        <td>
                            @Model.BillingAddress.CountryName
                        </td>
                    </tr>
                }
                @if (!String.IsNullOrEmpty(Model.BillingAddress.FormattedCustomAddressAttributes))
                {
                    <tr class="custom-attributes-view">
                        <td colspan="2">
                            @Html.Raw(Model.BillingAddress.FormattedCustomAddressAttributes)
                        </td>
                    </tr>
                }
                @if (!Model.IsLoggedInAsVendor)
                {
                    <tr>
                        <td colspan="2">
                            <a href="@Url.Action("AddressEdit", new { addressId = Model.BillingAddress.Id, orderId = Model.Id })" class="k-button"><span class="k-icon k-i-edit"></span>&nbsp;@T("Admin.Common.Edit")</a>
                        </td>
                    </tr>
                }
            </table>
        </p>
    </div>
</div>

@if (Model.IsShippable)
{
    <div class="card col-md-6">
        <div class="card-body">
            <h4 class="card-title">@T("Admin.Orders.ShippingInfo")</h4>
            <p class="card-text">

                @if (Model.ShippingAddress != null)
                {

                    <table class="table table-bordered table-striped table-hover">
                        @if (Model.ShippingAddress.FirstNameEnabled || Model.ShippingAddress.LastNameEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.FullName"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.FirstName @Model.ShippingAddress.LastName
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.EmailEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.Email"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.Email
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.PhoneEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.Phone"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.PhoneNumber
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.FaxEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.Fax"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.FaxNumber
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.CompanyEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.Company"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.Company
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.StreetAddressEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.Address1"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.Address1
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.StreetAddress2Enabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.Address2"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.Address2
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.CityEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.City"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.City
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.StateProvinceEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.StateProvince"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.StateProvinceName
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.ZipPostalCodeEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.ZipPostalCode"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.ZipPostalCode
                                </td>
                            </tr>
                        }
                        @if (Model.ShippingAddress.CountryEnabled)
                        {
                            <tr>
                                <td>
                                    @T("Admin.Orders.Address.Country"):
                                </td>
                                <td>
                                    @Model.ShippingAddress.CountryName
                                </td>
                            </tr>
                        }
                        @if (!String.IsNullOrEmpty(Model.ShippingAddress.FormattedCustomAddressAttributes))
                        {
                            <tr class="custom-attributes-view">
                                <td colspan="2">
                                    @Html.Raw(Model.ShippingAddress.FormattedCustomAddressAttributes)
                                </td>
                            </tr>
                        }
                        @if (!Model.IsLoggedInAsVendor)
                        {
                            <tr>
                                <td colspan="2">
                                    <a href="@Url.Action("AddressEdit", new { addressId = Model.ShippingAddress.Id, orderId = Model.Id })" class="k-button"><span class="k-icon k-i-edit"></span>&nbsp;@T("Admin.Common.Edit")</a>
                                </td>
                            </tr>
                        }
                    </table>
                    <div style="padding-top: 15px;">
                        <img alt="google maps" src="@Url.Content("~/administration/build/images/google-maps.gif")" />
                        <a href="@Html.Raw(Model.ShippingAddressGoogleMapsUrl)" target="_blank">@T("Admin.Orders.Fields.ShippingAddress.ViewOnGoogleMaps")</a>
                    </div>

                }
                @if (Model.PickupAddress != null)
                {
                    <div class="form-group">
                        <admin-label asp-for="PickupAddress" />
                        <div class="col-md-9 col-sm-9">
                            <table class="table table-bordered table-striped table-hover">
                                @if (!String.IsNullOrEmpty(Model.PickupAddress.Address1))
                                {
                                    <tr>
                                        <td>
                                            @T("Admin.Orders.Address.Address1"):
                                        </td>
                                        <td>
                                            @Model.PickupAddress.Address1
                                        </td>
                                    </tr>
                                }
                                @if (!String.IsNullOrEmpty(Model.PickupAddress.City))
                                {
                                    <tr>
                                        <td>
                                            @T("Admin.Orders.Address.City"):
                                        </td>
                                        <td>
                                            @Model.PickupAddress.City
                                        </td>
                                    </tr>
                                }
                                @if (!String.IsNullOrEmpty(Model.PickupAddress.ZipPostalCode))
                                {
                                    <tr>
                                        <td>
                                            @T("Admin.Orders.Address.ZipPostalCode"):
                                        </td>
                                        <td>
                                            @Model.PickupAddress.ZipPostalCode
                                        </td>
                                    </tr>
                                }
                                @if (!String.IsNullOrEmpty(Model.PickupAddress.CountryName))
                                {
                                    <tr>
                                        <td>
                                            @T("Admin.Orders.Address.Country"):
                                        </td>
                                        <td>
                                            @Model.PickupAddress.CountryName
                                        </td>
                                    </tr>
                                }
                            </table>
                        </div>
                    </div>
                }

                @if (Model.IsShippable)
                {
                    <div class="form-horizontal">
                        <div class="form-body">

                            <div class="form-group">
                                <admin-label asp-for="ShippingMethod" />
                                <div class="col-md-9 col-sm-9">
                                    <label class="control-label" id="lblShippingMethod">@Model.ShippingMethod</label>
                                    @if (!Model.IsLoggedInAsVendor)
                                    {
                                        <script type="text/javascript">
                                            $(document).ready(function () {
                                                toggleEditShippingMethod(false);
                                            });


                                            function toggleEditShippingMethod(editmode) {
                                                if (editmode) {
                                                    $('#lblShippingMethod').hide();
                                                    $('#divShippingMethod').show();
                                                    $('#btnEditShippingMethod').hide();
                                                    $('#btnSaveShippingMethod').show();
                                                    $('#btnCancelShippingMethod').show();
                                                } else {
                                                    $('#lblShippingMethod').show();
                                                    $('#divShippingMethod').hide();
                                                    $('#btnEditShippingMethod').show();
                                                    $('#btnSaveShippingMethod').hide();
                                                    $('#btnCancelShippingMethod').hide();
                                                }
                                            }

                                        </script>
                                        <div id="divShippingMethod" style="display: inline-block;"><admin-input asp-for="ShippingMethod" /></div>
                                        <button type="submit" id="btnEditShippingMethod" onclick=" toggleEditShippingMethod(true);return false; " class="k-button"><span class="k-icon k-i-edit"></span>&nbsp;@T("Admin.Common.Edit")</button>
                                        <input type="submit" value="@T("Admin.Common.Save")" id="btnSaveShippingMethod" name="save-shipping-method" class="k-button" onclick=" return confirm('@T("Admin.Common.AreYouSure")'); " />
                                        <input type="submit" value="@T("Admin.Common.Cancel")" onclick=" toggleEditShippingMethod(false);return false; " id="btnCancelShippingMethod" class="k-button" />
                                    }
                                    @if (!string.IsNullOrEmpty(Model.ShippingAdditionDescription))
                                    {
                                        <br />
                                        <label>@Html.Raw(Model.ShippingAdditionDescription)</label>
                                    }
                                </div>
                            </div>
                            @if (!Model.IsLoggedInAsVendor)
                            {
                                <div class="form-group">
                                    <admin-label asp-for="ShippingStatus" />
                                    <div class="col-md-9 col-sm-9">
                                        <label class="control-label">@Model.ShippingStatus</label>
                                    </div>
                                </div>
                            }
                        </div>
                    </div>
                }

            </p>
        </div>
    </div>

    @if (await permisionService.Authorize(Grand.Services.Security.PermissionSystemName.Shipments))
    {
        <div class="card col-md-12">
            <div class="card-body">
                <h4 class="card-title">@T("Admin.Orders.Shipments")</h4>
                <div class="card-text">
                    <div class="panel panel-default">
                        <vc:admin-widget widget-zone="order_details_shipping_top" additional-data="Model" />
                        <div class="panel-body">
                            <div id="shipments-grid"></div>
                        </div>
                        <div class="panel-footer">
                            @if (Model.CanAddNewShipments)
                            {
                                <button type="submit" id="btnAddNewShipment" name="btnAddNewShipment" onclick="javascript:setLocation('@(Url.Action("AddShipment", "Shipment", new { orderId = Model.Id }))'); return false;" class="k-button"><i class="fa fa-plus"></i>&nbsp;@T("Admin.Orders.Shipments.AddNew")</button>
                            }
                        </div>
                        <vc:admin-widget widget-zone="order_details_shipping_bottom" additional-data="Model" />
                    </div>
                </div>
            </div>
        </div>
        
       
        <script>
        $(document).ready(function () {
            $("#shipments-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("ShipmentsByOrder", "Shipment", new { orderId = Model.Id }))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    numeric: false,
                    previousNext: false,
                    info: false
                },
                //details grid
                detailInit: detailInit,
                scrollable: false,
                columns: [{
                    field: "ShipmentNumber",
                    title: "@T("Admin.Orders.Shipments.ID")",
                    width: 50,
                    template: '<a class="k-link" href="@Url.Content("~/Admin/Shipment/ShipmentDetails/")#=Id#">#=ShipmentNumber#</a>'
                }, {
                    field: "TrackingNumber",
                    title: "@T("Admin.Orders.Shipments.TrackingNumber")",
                    width: 100,
                        template: '<a class="k-link" href="@Url.Content("~/Admin/Shipment/ShipmentDetails/")#=Id#">#=kendo.htmlEncode(TrackingNumber)#</a>'
                }, {
                    field: "TotalWeight",
                    title: "@T("Admin.Orders.Shipments.TotalWeight")",
                    width: 100
                }, {
                    field: "ShippedDate",
                    title: "@T("Admin.Orders.Shipments.ShippedDate")",
                    width: 200,
                    format: "{0:G}"
                }, {
                    field: "DeliveryDate",
                    title: "@T("Admin.Orders.Shipments.DeliveryDate")",
                    width: 200,
                    format: "{0:G}"
                }]
            });
        });

        //details grid
        function detailInit(e) {
            $("<div/>").appendTo(e.detailCell).kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("ShipmentsItemsByShipmentId", "Shipment"))?shipmentId="+e.data.Id,
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                scrollable: false,
                sortable: true,
                pageable: {
                    refresh: true,
                    numeric: false,
                    previousNext: false,
                    info: false
                },
                columns: [
                    {
                        field: "ProductName",
                        title: "@T("Admin.Orders.Shipments.Products.ProductName")",
                        width: 400
                    },{
                        field: "ShippedFromWarehouse",
                        title: "@T("Admin.Orders.Shipments.Products.Warehouse")",
                        width: 150
                    },{
                        field: "QuantityInThisShipment",
                        title: "@T("Admin.Orders.Shipments.Products.QtyShipped")",
                        width: 150
                    },{
                        field: "ItemWeight",
                        title: "@T("Admin.Orders.Shipments.Products.ItemWeight")",
                        width: 150
                    },{
                        field: "ItemDimensions",
                        title: "@T("Admin.Orders.Shipments.Products.ItemDimensions")",
                        width: 150
                    }
                ]
            });
        }
        </script>
    }

}
else
{
        <div class="card col-md-6">
            <div class="note note-info">@T("Admin.Orders.ShippingInfo.NotRequired")</div>
        </div>
    
}

<vc:admin-widget widget-zone="order_details_address_bottom" additional-data="Model" />